<?php
include 'include/functions.php';
//set session save path
session_save_path($session_path);
// Initialize a session. 
session_start();
$count++;
$sessionId = session_id();
if((!$userID) || (!$start))
{
	header("Location: schedule.php"); /* Redirect browser */
	/* Make sure that code below does not get executed when we redirect. */
	exit;
}
include 'include/QuickPicks.php';
?>
<!DOCTYPE html PUBLIC "-//W3C//Dtd html 4.01 Transitional//EN"
"http://www.w3.org/tr/html4/loose.dtd">
<html><!-- InstanceBegin template="/Templates/MAIN.dwt.php" codeOutsideHTMLIsLocked="false" -->
  <head>
    <!-- InstanceBeginEditable name="doctitle" -->
    <title>JoshHughes.com NFL Football Pool</title>
    <!-- InstanceEndEditable -->
	<link rel="stylesheet" href="css/football.css" type="text/css">
    <script src="scripts/prototype.js" type="text/javascript"></script>
	<script src="scripts/scriptaculous.js" type="text/javascript"></script>
	<script type="text/javascript">
	// Dynamic Iframe loader
	function loadmonkeyframe(theURL)
	{
		document.getElementById("monkeyframe").src=theURL;
	}
	function loadpollframe(theURL)
	{
		document.getElementById("pollframe").src=theURL;
	}
	
	// resizes Iframe according to content
	function resizeMe(obj){ 
	docHeight = obj.document.body.scrollHeight
	 obj.style.height = docHeight + 5 + 'px'
	 } 
	</script>
    <!-- InstanceBeginEditable name="head" --><!-- InstanceEndEditable -->
  </head>
<body>
<div id="pagewidth" >
	<div id="header" >
	
		<?PHP include_once("layout/header.html"); ?>
		
	</div>
			<div id="wrapper" class="clearfix" > 
			<div id="maincol" >

			<!-- InstanceBeginEditable name="Contents" -->

			<?php			  
				if((time() - $start)  > 1200)//if session is more than 20 mins old
				{	//logout: DO NOT SUBMIT PICKS!!!
				?>
					<SCRIPT language="JavaScript">
						<!--
						window.location="logoff.php?timeout=true";
						//-->
					</SCRIPT>
				<?
				}
				else
				{
					if ($userID)
					{
						//Connect To Database
						dbConnect();
						
						//Get this week
						$CurrWeek = getWeek();
						
						$CanChangePick = array_fill(1, 16, true);
						
						for ($Num = 1; $Num <= 16; $Num++)
						{
							$sql_string = "SELECT PickEmailed, Home, Away FROM NFLSchedule WHERE GameNum = $Num AND Week = $Week";
							$result = mysql_query($sql_string) or die("Cannot query the database.<br/>" . mysql_error());
							$myrow = mysql_fetch_array($result);
							if ($myrow["Home"] !="")
							{
								$HomeTeams[$Num] = $myrow["Home"];
								$AwayTeams[$Num] = $myrow["Away"];
								if ($myrow["PickEmailed"] == 1)
									$CanChangePick[$Num] = false;
							}
						}
						//Set DoQuery flag to false, if no pick were passed there is no need to query
						$doQuery = false;
						$goodMagicNum = true;
						$result = mysql_query("SELECT MagicNum FROM NFLPicks WHERE Week=$Week AND userID <> $userID") or die("Cannot query the database.<br>" . mysql_error());
						do {
							if (($myrow["MagicNum"] != NULL) && ($myrow["MagicNum"] == $MagicNum))
							{
								echo "Somone geuessed ".$myrow["MagicNum"]."<br/>";
								$goodMagicNum = false;
							}					
						} while($myrow = mysql_fetch_array($result));
						
						
						
						echo "<H1>Your Week $Week Picks</H1>";										
						if ($Submit == "Submit Picks")
						{
							//Write SQL statment to insert values
							$SQL = "INSERT INTO NFLPicks (UserID, Week";
							if ($Game1 != "") {
								if ($CanChangePick[1] == true) {
									$SQL = $SQL.", Game1";
									$doQuery = true;
								} else {
									echo "Game1 pick ($AwayTeams[1] @ $HomeTeams[1]) cannot be updated, this game is already over or in progress.<br/>";
								}
							} 
							if ($Game2 != "") {
								if ($CanChangePick[2] == true) {
									$SQL = $SQL.", Game2";
									$doQuery = true;
								} else {
									echo "Game2 pick ($AwayTeams[2] @ $HomeTeams[2]) cannot be updated, this game is already over or in progress.<br/>";
								}
							}
							if ($Game3 != "") {
								if ($CanChangePick[3] == true) {
									$SQL = $SQL.", Game3";
									$doQuery = true;
								} else {
									echo "Game3 pick ($AwayTeams[3] @ $HomeTeams[3]) cannot be updated, this game is already over or in progress.<br/>";
								}
							}
							if ($Game4 != "") {
								if ($CanChangePick[4] == true) {
									$SQL = $SQL.", Game4";
									$doQuery = true;
								} else {
									echo "Game4 pick ($AwayTeams[4] @ $HomeTeams[4]) cannot be updated, this game is already over or in progress.<br/>";
								}
							}
							if ($Game5 != "") {
								if ($CanChangePick[5] == true) {
									$SQL = $SQL.", Game5";
									$doQuery = true;
								} else {
									echo "Game5 pick ($AwayTeams[5] @ $HomeTeams[5]) cannot be updated, this game is already over or in progress.<br/>";
								}
							}
							if ($Game6 != "") {
								if ($CanChangePick[6] == true) {
									$SQL = $SQL.", Game6";
									$doQuery = true;
								} else {
									echo "Game6 pick ($AwayTeams[6] @ $HomeTeams[6]) cannot be updated, this game is already over or in progress.<br/>";
								}
							}
							if ($Game7 != "") {
								if ($CanChangePick[7] == true) {
									$SQL = $SQL.", Game7";
									$doQuery = true;
								} else {
									echo "Game7 pick ($AwayTeams[7] @ $HomeTeams[7]) cannot be updated, this game is already over or in progress.<br/>";
								}
							}
							if ($Game8 != "") {
								if ($CanChangePick[8] == true) {
									$SQL = $SQL.", Game8";
									$doQuery = true;
								} else {
									echo "Game8 pick ($AwayTeams[8] @ $HomeTeams[8]) cannot be updated, this game is already over or in progress.<br/>";
								}
							}
							if ($Game9 != "") {
								if ($CanChangePick[9] == true) {
									$SQL = $SQL.", Game9";
									$doQuery = true;
								} else {
									echo "Game9 pick ($AwayTeams[9] @ $HomeTeams[9]) cannot be updated, this game is already over or in progress.<br/>";
								}
							}
							if ($Game10 != "") {
								if ($CanChangePick[10] == true) {
									$SQL = $SQL.", Game10";
									$doQuery = true;
								} else {
									echo "Game10 pick ($AwayTeams[10] @ $HomeTeams[10]) cannot be updated, this game is already over or in progress.<br/>";
								}
							}
							if ($Game11 != "") {
								if ($CanChangePick[11] == true) {
									$SQL = $SQL.", Game11";
									$doQuery = true;
								} else {
									echo "Game11 pick ($AwayTeams[11] @ $HomeTeams[11]) cannot be updated, this game is already over or in progress.<br/>";
								}
							}
							if ($Game12 != "") {
								if ($CanChangePick[12] == true) {
									$SQL = $SQL.", Game12";
									$doQuery = true;
								} else {
									echo "Game12 pick ($AwayTeams[12] @ $HomeTeams[12]) cannot be updated, this game is already over or in progress.<br/>";
								}
							}
							if ($Game13 != "") {
								if ($CanChangePick[13] == true) {
									$SQL = $SQL.", Game13";
									$doQuery = true;
								} else {
									echo "Game13 pick ($AwayTeams[13] @ $HomeTeams[13]) cannot be updated, this game is already over or in progress.<br/>";
								}
							}
							if ($Game14 != "") {
								if ($CanChangePick[14] == true) {
									$SQL = $SQL.", Game14";
									$doQuery = true;
								} else {
									echo "Game14 pick ($AwayTeams[14] @ $HomeTeams[14]) cannot be updated, this game is already over or in progress.<br/>";
								}
							}
							if ($Game15 != "") {
								if ($CanChangePick[15] == true) {
									$SQL = $SQL.", Game15";
									$doQuery = true;
								} else {
									echo "Game15 pick ($AwayTeams[15] @ $HomeTeams[15]) cannot be updated, this game is already over or in progress.<br/>";
								}
							}
							if ($Game16 != "") {
								if ($CanChangePick[16] == true) {
									$SQL = $SQL.", Game16";
									$doQuery = true;
								} else {
									echo "Game16 pick ($AwayTeams[16] @ $HomeTeams[16]) cannot be updated, this game is already over or in progress.<br/>";
								}
							}							
							if (($MagicNum != "") && ($goodMagicNum == true)) {
									$SQL = $SQL.", MagicNum";
									$doQuery = true;
							}
							$SQL = $SQL.") VALUES ($UserID, $Week";
							if (($Game1 != "") && ($CanChangePick[1] == true)) {
								$SQL = $SQL.", '$Game1'";
							}
							if (($Game2 != "") && ($CanChangePick[2] == true)) {
								$SQL = $SQL.", '$Game2'";
							}
							if (($Game3 != "") && ($CanChangePick[3] == true)) {
								$SQL = $SQL.", '$Game3'";
							}
							if (($Game4 != "") && ($CanChangePick[4] == true)) {
								$SQL = $SQL.", '$Game4'";
							}
							if (($Game5 != "") && ($CanChangePick[5] == true)) {
								$SQL = $SQL.", '$Game5'";
							}
							if (($Game6 != "") && ($CanChangePick[6] == true)) {
								$SQL = $SQL.", '$Game6'";
							}
							if (($Game7 != "") && ($CanChangePick[7] == true)) {
								$SQL = $SQL.", '$Game7'";
							}
							if (($Game8 != "") && ($CanChangePick[8] == true)) {
								$SQL = $SQL.", '$Game8'";
							}
							if (($Game9 != "") && ($CanChangePick[9] == true)) {
								$SQL = $SQL.", '$Game9'";
							}
							if (($Game10 != "") && ($CanChangePick[10] == true)) {
								$SQL = $SQL.", '$Game10'";
							}
							if (($Game11 != "") && ($CanChangePick[11] == true)) {
								$SQL = $SQL.", '$Game11'";
							}
							if (($Game12 != "") && ($CanChangePick[12] == true)) {
								$SQL = $SQL.", '$Game12'";
							}
							if (($Game13 != "") && ($CanChangePick[13] == true)) {
								$SQL = $SQL.", '$Game13'";
							}
							if (($Game14 != "") && ($CanChangePick[14] == true)) {
								$SQL = $SQL.", '$Game14'";
							}
							if (($Game15 != "") && ($CanChangePick[15] == true)) {
								$SQL = $SQL.", '$Game15'";
							}
							if (($Game16 != "") && ($CanChangePick[16] == true)) {
								$SQL = $SQL.", '$Game16'";
							}
							if (($MagicNum != "") &&  ($goodMagicNum == true)) {
								$SQL = $SQL.", '$MagicNum'";
							}
							$SQL = $SQL.")";
						}
						elseif ($QP == "Quick Picks")
						{
							makeUserQuickPicks($UserID, $Week);
						}
						elseif ($Submit == "Update Picks")
						{
							//Write SQL statment to insert values
							$SQL = "UPDATE NFLPicks SET ";
							if ($Game1 != "") {
								if ($CanChangePick[1] == true) {
									$SQL = $SQL."Game1 = '$Game1', ";
									$doQuery = true;
								} else {
									echo "Game1 pick ($AwayTeams[1] @ $HomeTeams[1]) cannot be updated, this game is already over or in progress.<br/>";
								}
							} 
							if ($Game2 != "") {
								if ($CanChangePick[2] == true) {
									$SQL = $SQL."Game2 = '$Game2', ";
									$doQuery = true;
								} else {
									echo "Game2 pick ($AwayTeams[2] @ $HomeTeams[2]) cannot be updated, this game is already over or in progress.<br/>";
								}
							}
							if ($Game3 != "") {
								if ($CanChangePick[3] == true) {
									$SQL = $SQL."Game3 = '$Game3', ";
									$doQuery = true;
								} else {
									echo "Game3 pick ($AwayTeams[3] @ $HomeTeams[3]) cannot be updated, this game is already over or in progress.<br/>";
								}
							} 
							if ($Game4 != "") {
								if ($CanChangePick[4] == true) {
									$SQL = $SQL."Game4 = '$Game4', ";
									$doQuery = true;
								} else {
									echo "Game4 pick ($AwayTeams[4] @ $HomeTeams[4]) cannot be updated, this game is already over or in progress.<br/>";
								}
							} 
							if ($Game5 != "") {
								if ($CanChangePick[5] == true) {
									$SQL = $SQL."Game5 = '$Game5', ";
									$doQuery = true;
								} else {
									echo "Game5 pick ($AwayTeams[5] @ $HomeTeams[5]) cannot be updated, this game is already over or in progress.<br/>";
								}
							} 
							if ($Game6 != "") {
								if ($CanChangePick[6] == true) {
									$SQL = $SQL."Game6 = '$Game6', ";
									$doQuery = true;
								} else {
									echo "Game6 pick ($AwayTeams[6] @ $HomeTeams[6]) cannot be updated, this game is already over or in progress.<br/>";
								}
							} 
							if ($Game7 != "") {
								if ($CanChangePick[7] == true) {
									$SQL = $SQL."Game7 = '$Game7', ";
									$doQuery = true;
								} else {
									echo "Game7 pick ($AwayTeams[7] @ $HomeTeams[7]) cannot be updated, this game is already over or in progress.<br/>";
								}
							} 
							if ($Game8 != "") {
								if ($CanChangePick[8] == true) {
									$SQL = $SQL."Game8 = '$Game8', ";
									$doQuery = true;
								} else {
									echo "Game8 pick ($AwayTeams[8] @ $HomeTeams[8]) cannot be updated, this game is already over or in progress.<br/>";
								}
							} 
							if ($Game9 != "") {
								if ($CanChangePick[9] == true) {
									$SQL = $SQL."Game9 = '$Game9', ";
									$doQuery = true;
								} else {
									echo "Game9 pick ($AwayTeams[9] @ $HomeTeams[9]) cannot be updated, this game is already over or in progress.<br/>";
								}
							} 
							if ($Game10 != "") {
								if ($CanChangePick[10] == true) {
									$SQL = $SQL."Game10 = '$Game10', ";
									$doQuery = true;
								} else {
									echo "Game10 pick ($AwayTeams[10] @ $HomeTeams[10]) cannot be updated, this game is already over or in progress.<br/>";
								}
							} 
							if ($Game11 != "") {
								if ($CanChangePick[11] == true) {
									$SQL = $SQL."Game11 = '$Game11', ";
									$doQuery = true;
								} else {
									echo "Game11 pick ($AwayTeams[11] @ $HomeTeams[11]) cannot be updated, this game is already over or in progress.<br/>";
								}
							} 
							if ($Game12 != "") {
								if ($CanChangePick[12] == true) {
									$SQL = $SQL."Game12 = '$Game12', ";
									$doQuery = true;
								} else {
									echo "Game12 pick ($AwayTeams[12] @ $HomeTeams[12]) cannot be updated, this game is already over or in progress.<br/>";
								}
							} 
							if ($Game13 != "") {
								if ($CanChangePick[13] == true) {
									$SQL = $SQL."Game13 = '$Game13', ";
									$doQuery = true;
								} else {
									echo "Game13 pick ($AwayTeams[13] @ $HomeTeams[13]) cannot be updated, this game is already over or in progress.<br/>";
								}
							} 
							if ($Game14 != "") {
								if ($CanChangePick[14] == true) {
									$SQL = $SQL."Game14 = '$Game14', ";
									$doQuery = true;
								} else {
									echo "Game14 pick ($AwayTeams[14] @ $HomeTeams[14]) cannot be updated, this game is already over or in progress.<br/>";
								}
							} 
							if ($Game15 != "") {
								if ($CanChangePick[15] == true) {
									$SQL = $SQL."Game15 = '$Game15', ";
									$doQuery = true;
								} else {
									echo "Game15 pick ($AwayTeams[15] @ $HomeTeams[15]) cannot be updated, this game is already over or in progress.<br/>";
								}
							} 
							if ($Game16 != "") {
								if ($CanChangePick[16] == true) {
									$SQL = $SQL."Game16 = '$Game16', ";
									$doQuery = true;
								} else {
									echo "Game16 pick ($AwayTeams[16] @ $HomeTeams[16]) cannot be updated, this game is already over or in progress.<br/>";
								}
							}
							if (($MagicNum != "") &&  ($goodMagicNum == true)) {
								$SQL = $SQL."MagicNum = '$MagicNum', ";
								$doQuery = true;
							} 
							$SQL = substr($SQL, 0, strlen($SQL)-2);
							$SQL = $SQL." WHERE UserID = '$UserID' AND Week = $Week";
						}
						//Open Record Set using SQL Query
						if ($doQuery)
						{
							mysql_query($SQL) or die("Cannot query the database.<br/>" . mysql_error());
							$filename = "notes/week".$Week.".txt";
							$fp = fopen($filename,"a");
							fputs($fp,date('m-d-Y H:i:s')." - ".$SQL."\n");
							fclose($fp);
						}
						// take care of Survivor Pick
						if ($Survivor != "")
						{
							$ChangedSurvivor = true;
							$Update = mysql_query("SELECT Pick FROM NFLSurvivalPicks WHERE UserID=$UserID AND Week=$Week") or die("Cannot query the database.<br/>" . mysql_error());
							if($outerrow = mysql_fetch_array($Update))
							{
								// Check to see if previous picked game has ended (The user possibly got it wrong and wants to change to a later game)
								$isOver = mysql_query("SELECT PickEmailed FROM NFLSchedule WHERE Week=$Week AND (Home='".$outerrow["Pick"]."' OR Away='".$outerrow["Pick"]."') AND PickEMailed=0") or die("Cannot query the database.<br/>" . mysql_error());
								if (!mysql_fetch_array($isOver)) //Previously picked game is over, user has already lost and cannot change
								{
									echo "You had previously picked the <b><i>".$outerrow["Pick"]."</i></b> for your survivor pick.  That game has started or completed, therefore you cannot change this weeks survivor pick anymore.<br/><br/>";
									$ChangedSurvivor = false;
								}
								else	//User has not lost and wants to change his/her pick
								{
									// Check to see if picked game has ended (The user wants to pick a sure thing)
									$isOver = mysql_query("SELECT PickEmailed FROM NFLSchedule WHERE Week=$Week AND (Home='$Survivor' OR Away='$Survivor') AND PickEMailed=0") or die("Cannot query the database.<br/>" . mysql_error());
									if (!mysql_fetch_array($isOver))	//User picked a game in progress or worse yet OVER.
									{
										echo "That game in which the <b><i>$Survivor</i></b> are playing has started or completed, therefore you cannot use this game for your survivor pick anymore.  Please select a team from a later game.<br/><br/>";
										$ChangedSurvivor = false;
									}
									else	// Else Update to the new Pick
										mysql_query("UPDATE NFLSurvivalPicks SET Pick='$Survivor' WHERE UserID=$UserID AND Week=$Week") or die("Cannot query the database.<br/>" . mysql_error());
								}
							}
							else
							{
								$isOver = mysql_query("SELECT PickEmailed FROM NFLSchedule WHERE Week=$Week AND (Home='$Survivor' OR Away='$Survivor') AND PickEMailed=0") or die("Cannot query the database.<br/>" . mysql_error());
								if (!mysql_fetch_array($isOver))
								{
									echo "That game in which the <b><i>$Survivor</i></b> are playing has started or completed, therefore you cannot use this game for your survivor pick anymore.  Please select a team from a later game.<br/><br/>";
									$ChangedSurvivor = false;
								}
								else	// Else Insert to the new Pick
									mysql_query("INSERT INTO NFLSurvivalPicks VALUES($UserID,$Week,'$Survivor')") or die("Cannot query the database.<br/>" . mysql_error());
							}
						}
						
						?><link rel="stylesheet" href="css/NFLPool.css" type="text/css"><?
						if ($QP != "Quick Picks")
						{
							if (($Game1 != "") && ($CanChangePick[1] == true)) {echo "Game 1 pick = $Game1<br/>";}
							if (($Game2 != "") && ($CanChangePick[2] == true)) {echo "Game 2 pick = $Game2<br/>";}
							if (($Game3 != "") && ($CanChangePick[3] == true)) {echo "Game 3 pick = $Game3<br/>";}
							if (($Game4 != "") && ($CanChangePick[4] == true)) {echo "Game 4 pick = $Game4<br/>";}
							if (($Game5 != "") && ($CanChangePick[5] == true)) {echo "Game 5 pick = $Game5<br/>";}
							if (($Game6 != "") && ($CanChangePick[6] == true)) {echo "Game 6 pick = $Game6<br/>";}
							if (($Game7 != "") && ($CanChangePick[7] == true)) {echo "Game 7 pick = $Game7<br/>";}
							if (($Game8 != "") && ($CanChangePick[8] == true)) {echo "Game 8 pick = $Game8<br/>";}
							if (($Game9 != "") && ($CanChangePick[9] == true)) {echo "Game 9 pick = $Game9<br/>";}
							if (($Game10 != "") && ($CanChangePick[10] == true)) {echo "Game 10 pick = $Game10<br/>";}
							if (($Game11 != "") && ($CanChangePick[11] == true)) {echo "Game 11 pick = $Game11<br/>";}
							if (($Game12 != "") && ($CanChangePick[12] == true)) {echo "Game 12 pick = $Game12<br/>";}
							if (($Game13 != "") && ($CanChangePick[13] == true)) {echo "Game 13 pick = $Game13<br/>";}
							if (($Game14 != "") && ($CanChangePick[14] == true)) {echo "Game 14 pick = $Game14<br/>";}
							if (($Game15 != "") && ($CanChangePick[15] == true)) {echo "Game 15 pick = $Game15<br/>";}
							if (($Game16 != "") && ($CanChangePick[16] == true)) {echo "Game 16 pick = $Game16<br/>";}
							if (($Survivor != "") && ($ChangedSurvivor == true))  {echo "<br/>Week $Week Survivor pick = $Survivor<br/>";}
							if ($goodMagicNum == true)
								echo "<br/><br/>Week $Week Magic Number guess = $MagicNum<br/>";
							else
								echo "<br/><br/><span style=\"color: #ff0000; font-weight: bold;\">* * * &nbsp;A Magic Number guess of $MagicNum has already been selected for Week $Week by another user.  Please <a href=\"picks.php?Weeknum=$Week\">go back</a> and submit another guess.&nbsp; * * *</span><br/>";
						}
						echo "<br/><a href=\"picks.php?Weeknum=$Week\" class=\"permLink\">Go To &quot;Picks&quot; Page</a>";
						echo "<br/><a href=\"weighpicks.php?Weeknum=$Week\" class=\"permLink\">Go To &quot;Weigh Picks&quot; Page</a>";
						echo "<br/><a href=\"survivor.php?Weeknum=$Week\" class=\"permLink\">Go To &quot;Survivor Picks&quot; Page</a>";
					}
					else		//If no $userID
					{
						echo "<H2>You must be registered member of this pool site, and you must be logged in to submit picks.</H1><p></p>";
						echo "<p>If you are a registered member, and you are logged in, then you must not of made the playoffs!";
					}//end if ($UserID)
			}//end if (time() - $start) < 1200
	?>
			<!-- InstanceEndEditable -->
			
			</div>
			<div id="leftcol" >
			
				<?PHP include_once("layout/left_column.php"); ?>
		
			</div>
			
		</div>
		
		<div id="footer" >
		
			<?PHP include_once("layout/footer.html"); ?>
			
		</div>
</div>
</body>
<!-- InstanceEnd --></html>